package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.common.AppJwtUtil;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {


    @Override
    public WmUser saveWmUser(WmUser wmUser) {
        if(wmUser==null){
            return null;
        }
        wmUser.setCreatedTime(new Date());
        boolean b = this.save(wmUser);
        if(b){
            return wmUser;
        }else{
            return null;
        }
    }

    @Override
    public WmUser findByName(String name) {
        if(StringUtils.isBlank(name)){
            return null;
        }

//        LambdaQueryWrapper<WmUser> lambdaQueryWrapper =new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(WmUser::getName,name);
//
//        QueryWrapper<WmUser> queryWrapper =new QueryWrapper<>();
//        queryWrapper.lambda().eq(WmUser::getName,name);
//
//        Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name);

        List<WmUser> wmUserList = this.list(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        if(wmUserList.size()>0){
            return  wmUserList.get(0);
        }
        return null;
    }

    @Override
    public ResponseResult login(WmUserDto dto) {
        //1：校验参数
        if(dto==null || StringUtils.isBlank(dto.getName())||StringUtils.isBlank(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2:根据用户名去数据库查询用户
        QueryWrapper<WmUser>  queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(WmUser::getName,dto.getName());
        List<WmUser> wmUserList = this.list(queryWrapper);

        if(wmUserList.size()>0){
            WmUser wmUser = wmUserList.get(0);

            //3:用户输入密码（参数中密码）+数据库中查询出来的盐值  生成一个新的md5密码串
            String newPwd = DigestUtils.md5DigestAsHex((dto.getPassword().concat(wmUser.getSalt()).getBytes()));
            //4: 数据库中密码串 比较  新生成一个新的md5密码串  true  生成token  把token传到前端   false 反之
            if(wmUser.getPassword().equals(newPwd)){

                //生成token
                Map<String,Object> map =new HashMap<>();
                map.put("token", AppJwtUtil.getToken(wmUser.getId().longValue()));
                wmUser.setPassword("");
                wmUser.setSalt("");
                map.put("user",wmUser);
                return ResponseResult.okResult(map);
            }else{
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
    }

    @Override
    public WmUser findByUserId(Integer userId) {
        return this.getById(userId);
    }
}
